package com.koron.order.application.controller;


import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.WorkbookUtil;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.ExportQuery;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.application.bean.dto.ApplicationDTO;
import com.koron.order.application.bean.po.ApplicationDelay;
import com.koron.order.application.bean.po.ApplicationDesc;
import com.koron.order.application.bean.query.ApplicationQuery;
import com.koron.order.application.bean.vo.ApplicationListVO;
import com.koron.order.application.service.ApplicationService;
import com.koron.order.application.tools.ApplicationCodeTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 检修申请工单(Application)表控制层
 *
 * @author jzp
 * @since 2021-09-27 16:43:09
 */
@Api(tags = "检修申请工单接口")
@RestController
@RequestMapping("application")
public class ApplicationController {
    /**
     * 服务对象
     */
    @Autowired
    private ApplicationService applicationService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<PageInfo> page(PageQuery pageQuery, ApplicationQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), applicationService, "page", PageInfo.class, pageQuery, query));
    }

    @ApiOperation(value = "列表查询")
    @GetMapping("list")
    public Response<List<ApplicationListVO>> list(ApplicationQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), applicationService, "list", List.class, query));
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<ApplicationDTO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), applicationService, "getDtoById", ApplicationDTO.class, id));
    }

    @ApiOperation(value = "查询批复记录", notes = "根据检修申请单id查询批复记录")
    @GetMapping("listDelay")
    public Response<List<ApplicationDelay>> listDelay(String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), applicationService, "listDelay", List.class, id));
    }

    @ApiOperation(value = "查询备注记录", notes = "根据检修申请单id查询备注记录")
    @GetMapping("listDesc")
    public Response<List<ApplicationDesc>> listDesc(String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), applicationService, "listDesc", List.class, id));
    }

    @ApiOperation(value = "保存或更新数据", notes = "只有草稿状态有保存功能")
    @PostMapping
    public Response<String> save(@RequestBody ApplicationDTO applicationDTO) {
        ADOConnection.runTask(getCurrDs(), applicationService, "saveOrUpdate", Boolean.class, applicationDTO);
        return Response.ok(applicationDTO.getId());
    }

    @ApiOperation(value = "提交数据")
    @PostMapping("submit")
    public Response<String> submit(@RequestBody ApplicationDTO applicationDTO) {
        ADOConnection.runTask(getCurrDs(), applicationService, "submit", Boolean.class, applicationDTO);
        return Response.ok("提交成功", applicationDTO.getId());
    }

    @ApiOperation(value = "延时提交")
    @PostMapping("delaySubmit")
    public Response<String> delaySubmit(@RequestBody ApplicationDelay applicationDelay) {
        ADOConnection.runTask(getCurrDs(), applicationService, "delaySubmit", Boolean.class, applicationDelay);
        return Response.ok("提交成功", applicationDelay.getId());
    }

    @ApiOperation(value = "保存备注记录")
    @PostMapping("saveDesc")
    public Response<String> saveDesc(@RequestBody ApplicationDesc applicationDesc) {
        ADOConnection.runTask(getCurrDs(), applicationService, "saveDesc", Boolean.class, applicationDesc);
        return Response.ok(applicationDesc.getId());
    }

    @ApiOperation(value = "作废数据")
    @PutMapping("invalidate")
    public Response<String> invalidate(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), applicationService, "invalidate", Boolean.class, id);
        return Response.ok("作废成功", null);
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), applicationService, "deleteById", Boolean.class, id);
        return Response.ok();
    }

    @SneakyThrows
    @ApiOperation(value = "导出数据")
    @GetMapping("export")
    public void export(ExportQuery query, HttpServletResponse response) {
        String path = "excel/application.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        Workbook book = WorkbookUtil.createBook(classPathResource.getInputStream());
        ExcelWriter writer = new ExcelWriter(book, "检修申请单");
        String fileName = "repairApplication"+ApplicationCodeTools.getApplicationExportCode();

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+ fileName +".xlsx");

        List<?> exportList = ADOConnection.runTask(getCurrDs(), applicationService, "exportList", List.class, query);
        writer.passRows(3);
        writer.write(exportList, false);

        ServletOutputStream out=response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }

}
